unit GLGUIWidgetSkin;

interface

uses
  VectorGeometry, dialogs,
  VectorTypes,
  GLSCrossXML,
  GLSGenerics,
  GLGUISkin,
  GLGUIState,
  GLGUISkinManager,
  GLGUITokens,
  GLGUIUtils;

type
  TGLGUISubWidgetInfo = class;

  TGLGUISubWidgetInfoList = GList<TGLGUISubWidgetInfo>;

  TGLGUISubWidgetInfo = class
  public
    Name: String;
    TypeName: String;
    SkinName: String;
    Align: TGLGUIAlign;
    Width: Single;
    Height: Single;
    HasSubWidgets: Boolean;
    SubWidgets: TGLGUISubWidgetInfoList;
  end;

  TGLGUIWidgetSkin = class(TGLGUICustomSkin)
  protected
    FSubWidgets: TGLGUISubWidgetInfoList;
    function ParseSubWidget(const ANode: GLSXMLNode): TGLGUISubWidgetInfo;
    function GetSubWidgetInfo(AIndex: Integer): TGLGUISubWidgetInfo;
    function GetSubWidgetsCount: Integer;
  public
    constructor Create; override;
    procedure Parse; override;
    destructor Destroy; override;
    property SubWidgets[AIndex: Integer]: TGLGUISubWidgetInfo read
      GetSubWidgetInfo;
    property SubWidgetsCount: Integer read GetSubWidgetsCount;
  end;

implementation

constructor TGLGUIWidgetSkin.Create;
begin
  inherited;
  FSubWidgets := TGLGUISubWidgetInfoList.Create;
end;

procedure TGLGUIWidgetSkin.Parse;
var
  i: Integer;
  vCurentSkinElement: GLSXMLNode;
  vCurrentValue: String;
begin
  if FXMLNode = nil then
    Exit;

  for i := 0 to FSubWidgets.Count - 1 do
    FSubWidgets[i].Free;
  FSubWidgets.Clear;

  for i := 0 to FXMLNode.ChildNodes.Count - 1 do
  begin
    vCurentSkinElement := FXMLNode.ChildNodes.Get(i);
    if vCurentSkinElement.LocalName = 'SubWidget' then
      FSubWidgets.Add(ParseSubWidget(vCurentSkinElement));
  end;

  FIsParsed := True;
end;

function TGLGUIWidgetSkin.ParseSubWidget(const ANode: GLSXMLNode): TGLGUISubWidgetInfo;
var
  vCurrentValue: String;
  vSize: TVector2f;
  vInfo: TGLGUISubWidgetInfo;
  vCurentSkinElement: GLSXMLNode;
  i: Integer;
begin
  vInfo := TGLGUISubWidgetInfo.Create;
  GetXMLAttribute(ANode, 'Name', vInfo.Name);
  GetXMLAttribute(ANode, 'Type', vInfo.TypeName);
  GetXMLAttribute(ANode, 'Skin', vInfo.SkinName);
  GetXMLAttribute(ANode, 'Align', vCurrentValue);
  vInfo.Align := StringToAlign(vCurrentValue);
  GetXMLAttribute(ANode, 'Size', vCurrentValue);
  vSize := StringtoVector2f(vCurrentValue);
  vInfo.Width := vSize[0];
  vInfo.Height := vSize[1];
  {

  for i := 0 to ANode.ChildNodes.Count - 1 do
  begin
    vCurentSkinElement := ANode.ChildNodes.Get(i);
    GetXMLAttribute(vCurentSkinElement, 'Value', vCurrentValue);
    if (vCurentSkinElement.LocalName = 'Align') then
      vInfo.Align := StringToAlign(vCurrentValue)
    else if (vCurentSkinElement.LocalName = 'Width') then
      vInfo.Width := StrToFloat(vCurrentValue)
    else if (vCurentSkinElement.LocalName = 'Height') then
      vInfo.Height := StrToFloat(vCurrentValue);
  end;
}
  Result := vInfo;
end;

function TGLGUIWidgetSkin.GetSubWidgetInfo(AIndex: Integer): TGLGUISubWidgetInfo;
begin
  Result := FSubWidgets.Items[AIndex];
end;

function TGLGUIWidgetSkin.GetSubWidgetsCount: Integer;
begin
  Result := FSubWidgets.Count;
end;

destructor TGLGUIWidgetSkin.Destroy;
var
  i: Integer;
begin
  for i := 0 to FSubWidgets.Count - 1 do
    FSubWidgets[i].Free;
  FSubWidgets.Free;
  inherited;
end;

initialization
  RegisterSkin(TGLGUIWidgetSkin);

finalization

end.
